package com.ruoyi.blog.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.blog.domain.AcLike;
import com.ruoyi.blog.service.IAcLikeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 点赞管理Controller
 * 
 * @author ruoyi
 * @date 2022-06-19
 */
@RestController
@RequestMapping("/blog/like")
public class AcLikeController extends BaseController
{
    @Autowired
    private IAcLikeService acLikeService;

    @GetMapping("/isLike")
    public AjaxResult isLike(AcLike acLike)
    {
        acLike.setUserId(getUserId());
        return toAjax(acLikeService.selectAcLikeList(acLike).size() > 0);
    }

    @PostMapping("/like")
    public AjaxResult like(@RequestBody AcLike acLike)
    {
        acLike.setUserId(getUserId());
        if (acLikeService.selectAcLikeList(acLike).size() > 0)
            return error("已点赞(踩)过");
        return toAjax(acLikeService.insertAcLike(acLike));
    }

    @PostMapping("/cancelLike")
    public AjaxResult cancelLike(@RequestBody AcLike acLike)
    {
        acLike.setUserId(getUserId());
        List<AcLike> list = acLikeService.selectAcLikeList(acLike);
        if (list.size() == 0)
            return error();
        acLike = list.get(0);
        return toAjax(acLikeService.deleteAcLikeById(acLike.getId()));
    }

    /**
     * 查询点赞管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:like:list')")
    @GetMapping("/list")
    public TableDataInfo list(AcLike acLike)
    {
        startPage();
        List<AcLike> list = acLikeService.selectAcLikeList(acLike);
        return getDataTable(list);
    }

    /**
     * 导出点赞管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:like:export')")
    @Log(title = "点赞管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcLike acLike)
    {
        List<AcLike> list = acLikeService.selectAcLikeList(acLike);
        ExcelUtil<AcLike> util = new ExcelUtil<AcLike>(AcLike.class);
        util.exportExcel(response, list, "点赞管理数据");
    }

    /**
     * 获取点赞管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:like:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(acLikeService.selectAcLikeById(id));
    }

    /**
     * 新增点赞管理
     */
    @PreAuthorize("@ss.hasPermi('blog:like:add')")
    @Log(title = "点赞管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AcLike acLike)
    {
        return toAjax(acLikeService.insertAcLike(acLike));
    }

    /**
     * 修改点赞管理
     */
    @PreAuthorize("@ss.hasPermi('blog:like:edit')")
    @Log(title = "点赞管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AcLike acLike)
    {
        return toAjax(acLikeService.updateAcLike(acLike));
    }

    /**
     * 删除点赞管理
     */
    @PreAuthorize("@ss.hasPermi('blog:like:remove')")
    @Log(title = "点赞管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(acLikeService.deleteAcLikeByIds(ids));
    }
}
